# @Author: 唐奇才
# @Time: 2021/5/24 20:27
# @File: 完整项目.py
# @Software: PyCharm

import requests
from SpiderUtils.myUtils import myFakerHeaders
from bs4 import BeautifulSoup



def get_html(url):
    header = {'User-Agent': myFakerHeaders.getFakeHeaders()}
    # p = myFakerHeaders.getFakeProxy()
    req = requests.get(url=url, headers=header)
    # 因为又是后是图片，就不要返回text了
    return req


def is_girls(tags):
    if "girl" in tags:
        return True
    elif "women" in tags:
        return True
    else:
        return False

def get_img(src):
    html = get_html(src)
    bs = BeautifulSoup(html.text, parser="lxml")
    try:
        img_src = bs.find("img", id="wallpaper")['src']
        return get_html(img_src)
    except:
        print(bs.find("img", id="wallpaper"))
        print("原来的获取图片出错")
        img_src = bs.find("img", id="wallpaper")['data-cfsrc']
        if img_src:
            return None
        return get_html(img_src)



def run():
    # # 1. 获取主页页面
    for i in range(3, 134):
        url = "https://wallhaven.cc/toplist?page={}".format(i)

        html = get_html(url)
        bs = BeautifulSoup(html.text, parser="lxml")
        sections = bs.find_all("section", class_="thumb-listing-page")
        for s in sections:
            imgs_url = s.find_all("li")
            for img in imgs_url:
                href = img.a['href']
                html1 = get_html(href)
                bs1 = BeautifulSoup(html1.text, parser="lxml")
                len(bs1.find_all("ul", id="tags"))
                tags = bs1.find_all("ul", id="tags")
                tags_str = ""
                for tag in tags:
                    ts = tag.find_all("li")
                    for t in ts:
                        tags_str += t.a.text
                    # 2. 进入页面，判断是否为女孩
                    if is_girls(tags_str):
                        name = href.split("/")[-1]
                        # 3. 进入下载
                        img = get_img(href).content
                        if img:
                            print("no image")
                            continue
                        with open("./images/{}-{}.png".format(i, name), "wb") as f:
                            f.write(img)
                            print(i, "下载完成！")




# class myThread (threading.Thread):
#     def __init__(self, threadID):
#         threading.Thread.__init__(self)
#         self.threadID = threadID
#
#     def run(self):
#         print ("开始线程：" , self.threadID)
#         run(self.threadID)
#         print ("退出线程：" , self.threadID)


def main():
    run()



if __name__ == '__main__':
    main()